// pages/personalCenter/personalCenter.js
//获取应用实例
const app = getApp()
var template = require('../../utils/tabBarTemplate/tabBarTemplate.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    session_key:'',
    userinfo:{},//个人信息
    teamlist:[],//球队信息,
    myData:{},//我的数据
    total_sign:0,//天数
    positionName:[],//位置的名字列表
    imgpath: app.globalData.imagePath
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 调用函数
    template.tabbar("tabBar", 4, this)//4表示第5个tabbar

    var that = this;
    that.setData({
      session_key: wx.getStorageSync("token")
    })
    wx.showLoading({
      title: '数据加载中'
    })
    //获取个人信息
    wx.request({
      url: app.globalData.httpsPath + '/user/info',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: that.data.session_key
      },
      //请求后台数据成功  
      success: function (res) {
        wx.hideLoading();
        var _info = res.data.data;
        that.setData({
          userinfo : _info,
          total_sign : _info.sign_num
        }) 
      }
    })
    //获取场上位置
    wx.request({
      url: app.globalData.httpsPath + '/position', // 接口地址
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        var posArry = [];
        var positionList = res.data.data;
        var _poslist_id = that.data.userinfo.position_id;
        for (var i = 0; i < positionList.length; i++) {
          posArry.push(positionList[i].name);
        }
        that.setData({
          positionName: posArry
        })
      }
    })
    //获取球队信息
    wx.request({
      url: app.globalData.httpsPath + '/user/team',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: that.data.session_key
      },
      //请求后台数据成功  
      success: function (res) {
        var _createTeam = res.data.data.data;
        if (_createTeam.length != 0){
          that.setData({
            teamlist: _createTeam
          })
        }
        
      }
    })
    //获取我的数据
    wx.request({
      url: app.globalData.httpsPath + '/user/my-data',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: that.data.session_key
      },
      //请求后台数据成功  
      success: function (res) {
        var _myData = res.data.data;
        that.setData({
          myData: _myData,
        })
      }
    })
  },
  //打卡
  punchClick:function(){
    var that = this;
    var _token = wx.getStorageSync("token");
    let _latitude, _longitude;
    //经纬度
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        _latitude = res.latitude // 纬度
        _longitude = res.longitude // 经度
      }
    })
    setTimeout(function () {
      wx.request({
        url: app.globalData.httpsPath + '/user/sign',
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        data: {
          token: _token,
          latitude: _latitude,
          longitude: _longitude
        },
        success: function (res) {
          if (res.data.status == true) {
            that.setData({
              total_sign: that.data.total_sign + 1,
              signed: true
            })
            wx.showToast({
              title: '打卡成功',
              icon: 'success',
              duration: 2000
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
        },
      })
    }, 1500)
    
  },
  qiuduijoin:function(){
    wx.navigateTo({
      url: '../myTeam/myTeam'
    })
  }
})